<?php
class Referal extends AppModel {

	var $name = 'Referal';
    var $primaryKey = 'referal_id';

    /**
     * Updating incoming referal
     * usage in Post Controller -> function index() , view()
     */
    function updateReferal($name){
        $this->recursive = -1;
        $check = $this->checkByName($name);
        if(!empty($check)){
            $this->updateAll(array('Referal.count'=>"Referal.count + 1"),array('Referal.referal_id'=>$check['Referal']['referal_id']));
        }else{
            $this->create();
            $this->data = array('name'=>$name);
            $this->save($this->data);            
        }
    }

    /**
     * Checking if referal exist or not
     */
    function checkByName($name) {
        $this->recursive = -1;
        $result = $this->find('first',array(
            'conditions'=>array('Referal.name'=>$name),
            'fields'=>array('Referal.referal_id')
        ));

        return $result;
    }

}
?>
